<?php

namespace App\Repositories\Impl;

use App\Models\Video;
use App\Repositories\Contracts\VideoRepository;
use App\Repositories\Transformers\VideoTransformer;

class VideoRepositoryImpl implements VideoRepository
{
    public function paginate($perPage, $filter)
    {
        $video = new Video();

        if (isset($filter['type']) && $filter['type'] > 0) {
            $video = $video->where('type', $filter['type']);
        }

        if (!empty($filter['keyword'])) {
            $video = $video->where('title', 'like', '%' . $filter['keyword'] . '%')
                ->orWhere('content', 'like', '%' . $filter['keyword'] . '%');
        }

        $video = $video->orderBy('created_at','desc')->paginate($perPage);
        $result = fractal($video, new VideoTransformer());

        return $result->toArray();

    }

    public function find($id, $columns = ['*'])
    {
        return Video::find($id, $columns);
    }

    public function findByFilter($filter, $columns = ['*'])
    {
        return Video::where($filter)->first($columns);
    }

    public function store($data)
    {
        $video = Video::create($data);

        $result = fractal($video, new VideoTransformer());
        return $result->toArray();
    }

    public function update($data, $id)
    {
        return Video::whereId($id)->update($data);
    }

    public function updateByFilter($data, $where)
    {
        return Video::where($where)->update($data);
    }

    public function delete($id)
    {
        return Video::whereId($id)->delete();
    }
}
